<?php
namespace app\api\model;

use think\Model;
use think\Validate;

class Firmware extends Model{
    protected $rules = [
        'version' => 'require',
    ];
	/**
	 * [createData 新建]
	 */
	public function createData($param)
	{
		// 验证
		$validate = new Validate($this->rules);
		if (!$validate->check($param)) {
			$this->error = $validate->getError();
			return false;
		}
		try {
            $param['create_time'] = time();
            $param['update_time'] = time();
			$this->data($param)->allowField(true)->save();
			return true;
		} catch(\Exception $e) {
			$this->error = $e->getMessage();
			return false;
		}
    }
    
    /**
     * 获取列表
     */
    public function list($param){
		$result = $this->limit($param['limit'])->page($param['page'])->order('id desc')->select();
		$total = $this->count();
		$list = [
			'list' => $result,
			'total' => $total
		];
        return $list;
    }

    /**
	 * 编辑
	 */
	public function updateDataById($param, $id)
	{
		$checkData = $this->get($id);
		if (!$checkData) {
			$this->error = '暂无此数据';
			return false;
		}
		try {
			$this->allowField(true)->save($param, [$this->getPk() => $id]);
			return true;
		} catch(\Exception $e) {
            var_dump($e->getMessage());
			$this->error = '编辑失败';
			return false;
		}
	}
}